package com.nf.health.app.healthbank;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.nf.health.app.R;
import com.nf.health.app.activity.global.BaseActivity;
import com.nf.health.app.core.MyGsonBuilder;
import com.nf.health.app.customview.DragListView;
import com.nf.health.app.customview.EmptyView;
import com.nf.health.app.healthbank.adapter.BankDoctorAdapter;
import com.nf.health.app.http.Act;
import com.nf.health.app.models.BankDoctorModel;
import com.nf.health.app.utils.DensityUtil;
import com.nf.health.app.utils.JsonUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 *获取健康银行下医生列表
 * @author  yyh
 *
 */
public class BanklPlaceDoctorListActivity extends BaseActivity {
    public static final String DOCTOR_ID = "BanklPlaceDoctorListActivity.doctorId";
    public static final String DOCTOR_NAME = "BanklPlaceDoctorListActivity.doctorName";
    public static final String DOCTOR_DEPARTMENT = "BanklPlaceDoctorListActivity.department";
  /*  @Bind(R.id.ll_nodata)
    LinearLayout ll_nodata;*/
    @Bind(R.id.titlebar_title_tv)
    TextView titlebar_title_tv;
    @Bind(R.id.tv_bankcontent)
    TextView tv_bankcontent;
    @Bind(R.id.ll_nodata)
    LinearLayout ll_nodata;
    @Bind(R.id.swipe_placedoctor_layout)
    SwipeRefreshLayout swipe_placedoctor_layout;
    @Bind(R.id.recyclerView_placedoctor)
    DragListView recyclerView_placedoctor;
    private boolean isPullToRefreshed = true;//是不是上拉刷新还是下拉加载
    private boolean mIsRefreshing = false;//是不是上拉刷新还是下拉加载
    private BankDoctorAdapter bankDoctorRecyAdapter;
    private List<BankDoctorModel> listSearchMainBean = new ArrayList<>();
    private int pageSize = 10;int pageNo =1;
    private String doctorId = "";
    private String doctorDoctor = "";
    private String department = "";
    private String totalCount;//共签约人数
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitleBar(R.layout.titlebar_base);
        setContent(R.layout.activity_placedoctorlist);
        ButterKnife.bind(this);
        initView();
        initListener();
        loadData(1);
    }

    private void initListener() {
        swipe_placedoctor_layout.setColorSchemeResources(R.color.theme_color, android.R.color.holo_red_light, android.R.color.holo_green_light, android.R.color.holo_orange_light);
        LinearLayoutManager mLayoutManager = new LinearLayoutManager(this);
        mLayoutManager.setOrientation(OrientationHelper.VERTICAL);
        initRecycleListener();
        bankDoctorRecyAdapter = new BankDoctorAdapter(this, listSearchMainBean);
        recyclerView_placedoctor.setAdapter(bankDoctorRecyAdapter);
        swipe_placedoctor_layout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                isPullToRefreshed = true;
                mIsRefreshing = true;
                pageNo = 1;
                loadData(pageNo);
            }
        });
        tv_bankcontent.setText("医生团队0名");
        recyclerView_placedoctor.setOnRefreshAndLoadMoreListener(new DragListView.onRefreshAndLoadMoreListener() {
            @Override
            public void onRefresh() { }

            @Override
            public void onLoadMore() {
                recyclerView_placedoctor.setRefreshableAndLoadMoreable(false, false);
                isPullToRefreshed = false;
                pageNo++;
                loadData(pageNo);
            }
        });
        bankDoctorRecyAdapter.notifyDataSetChanged();
    }

    private void loadData(int pageNo) {
        doctorId = getIntent().getStringExtra(DOCTOR_ID);
        doctorDoctor = getIntent().getStringExtra(DOCTOR_NAME);
        department = getIntent().getStringExtra(DOCTOR_DEPARTMENT);
        titlebar_title_tv.setText(""+doctorDoctor+"医院");
        provider.etAttentionBankDoctorList(doctorId,department,String.valueOf(pageNo),String.valueOf(pageSize));
    }

    @Override
    public void handleActionSuccess(String actionName, Object object) {
        super.handleActionSuccess(actionName, object);
        switch (actionName){
            case Act.Flag.GETATTENTIONBANKDOCTORLIST_FLG:
                recyclerView_placedoctor.completeLoadMore();
                totalCount = JsonUtils.getFiledData(object.toString(), "totalCount");
                String data = JsonUtils.getFiledData(object.toString(), "data");
                String list = JsonUtils.getFiledData(data, "list");
                 tv_bankcontent.setText("医生团队"+totalCount+"名");
                        Gson gson = new MyGsonBuilder().createGson();
                        List<BankDoctorModel> subListSearchMainBean = gson.fromJson(list, new TypeToken<List<BankDoctorModel>>() {
                        }.getType());
                if(isPullToRefreshed){
                    listSearchMainBean.clear();
                    listSearchMainBean.addAll(subListSearchMainBean);
                    if(subListSearchMainBean.size() < pageSize){
                        recyclerView_placedoctor.setRefreshableAndLoadMoreable(false, false);
                    }else{
                        recyclerView_placedoctor.setRefreshableAndLoadMoreable(false,true);
                    }
                    mIsRefreshing = false;
                }else{
                    listSearchMainBean.addAll(subListSearchMainBean);
                    if(subListSearchMainBean.size() < pageSize){
                        recyclerView_placedoctor.setRefreshableAndLoadMoreable(false, false);
                    }else{
                        recyclerView_placedoctor.setRefreshableAndLoadMoreable(false,true);
                    }
                }
                swipe_placedoctor_layout.setRefreshing(false);
                bankDoctorRecyAdapter.setDoctorCommentData(listSearchMainBean);
                setMyVisibile();
                  break;

        }
    }

    public void setMyVisibile() {
        if(listSearchMainBean == null || listSearchMainBean.size() == 0){

            EmptyView emptyView = new EmptyView(BanklPlaceDoctorListActivity.this).setOnRefreshData(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    pageNo = 1;
                    loadData(pageNo);
                }
            });
            LinearLayout.LayoutParams  params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

            LinearLayout.LayoutParams params1 =  new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            params1.height = DensityUtil.getScreenIntHeight(BanklPlaceDoctorListActivity.this);
            params1.width = DensityUtil.getScreenIntWidth(BanklPlaceDoctorListActivity.this);
            emptyView.setLayoutParams(params1);

            ll_nodata.setGravity(Gravity.CENTER);
            swipe_placedoctor_layout.setVisibility(View.GONE);
            ll_nodata.setVisibility(View.VISIBLE);
            ll_nodata.addView(emptyView);
            ll_nodata.setLayoutParams(params);
            tv_bankcontent.setVisibility(View.GONE);
        }else{
            tv_bankcontent.setVisibility(View.VISIBLE);
            swipe_placedoctor_layout.setVisibility(View.VISIBLE);
            ll_nodata.setVisibility(View.GONE);
        }
    }

    private void initView() {
        
    }

    public void initRecycleListener(){
        recyclerView_placedoctor.setOnTouchListener(
                new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        if (mIsRefreshing) {
                            return true;
                        } else {
                            return false;
                        }
                    }
                }
        );
    }
}
